package compare;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class Sys_Account_ab01 {

	private static Logger log = Logger.getLogger(" ");
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			log.error("驱动加载异常：" + e.getMessage());
		}
	}
	private static final String MMYSQL_LOCAL_SQL="select user_id,account from sys_user_account;";
	private static final String MYSQL_JYT_SQL="select count(1) from sys_user_account where account='{account}'";
	
	public static void main(String[] args) throws SQLException, IOException {
		long time = System.currentTimeMillis();
		Connection connLocal = DriverManager.getConnection(SqlJYTUtils.getMySqlLocalUrl());
		Statement sLocal = connLocal.createStatement();
		ResultSet rs = sLocal.executeQuery(MMYSQL_LOCAL_SQL);
		Connection connMysql = DriverManager.getConnection(SqlJYTUtils.getMySqlJytUrl());
		int index=0;
		while (rs.next()) {
			String user_id = rs.getString("user_id");
			String account = rs.getString("account");
			int count = getCountByName(connMysql, account);
			if (count > 0) {
				log.warn("mysql中存在ab01的数据,user_id=" + user_id + ",account=" + account);
				index++;
			}
		}
		log.info("已对比index="+index);
		connLocal.close();
		connMysql.close();
		log.info("按账户，全部对比完成，耗时:" + (System.currentTimeMillis() - time) + "ms");
	}

	private static int getCountByName(Connection conn, String account) throws SQLException {
		String sql = MYSQL_JYT_SQL.replace("{account}", account);
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		int count = 0;
		if (rs.next()) {
			count = rs.getInt(1);
		}
		rs.close();
		stmt.close();
		return count;
	}
	
}